package com.ruoyi.ldzlsb.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.ldzlsb.mapper.SbInspectionItemMapper;
import com.ruoyi.ldzlsb.domain.SbInspectionItem;
import com.ruoyi.ldzlsb.service.ISbInspectionItemService;

/**
 * 巡检项Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-15
 */
@Service
public class SbInspectionItemServiceImpl implements ISbInspectionItemService 
{
    @Autowired
    private SbInspectionItemMapper sbInspectionItemMapper;

    /**
     * 查询巡检项
     * 
     * @param itemId 巡检项主键
     * @return 巡检项
     */
    @Override
    public SbInspectionItem selectSbInspectionItemByItemId(Long itemId)
    {
        return sbInspectionItemMapper.selectSbInspectionItemByItemId(itemId);
    }

    /**
     * 查询巡检项列表
     * 
     * @param sbInspectionItem 巡检项
     * @return 巡检项
     */
    @Override
    public List<SbInspectionItem> selectSbInspectionItemList(SbInspectionItem sbInspectionItem)
    {
        return sbInspectionItemMapper.selectSbInspectionItemList(sbInspectionItem);
    }

    /**
     * 根据配置ID查询巡检项列表
     * 
     * @param configId 配置ID
     * @return 巡检项集合
     */
    @Override
    public List<SbInspectionItem> selectSbInspectionItemByConfigId(Long configId)
    {
        return sbInspectionItemMapper.selectSbInspectionItemByConfigId(configId);
    }

    /**
     * 新增巡检项
     * 
     * @param sbInspectionItem 巡检项
     * @return 结果
     */
    @Override
    public int insertSbInspectionItem(SbInspectionItem sbInspectionItem)
    {
        sbInspectionItem.setCreateTime(DateUtils.getNowDate());
        return sbInspectionItemMapper.insertSbInspectionItem(sbInspectionItem);
    }

    /**
     * 修改巡检项
     * 
     * @param sbInspectionItem 巡检项
     * @return 结果
     */
    @Override
    public int updateSbInspectionItem(SbInspectionItem sbInspectionItem)
    {
        sbInspectionItem.setUpdateTime(DateUtils.getNowDate());
        return sbInspectionItemMapper.updateSbInspectionItem(sbInspectionItem);
    }

    /**
     * 批量删除巡检项
     * 
     * @param itemIds 需要删除的巡检项主键
     * @return 结果
     */
    @Override
    public int deleteSbInspectionItemByItemIds(Long[] itemIds)
    {
        return sbInspectionItemMapper.deleteSbInspectionItemByItemIds(itemIds);
    }

    /**
     * 删除巡检项信息
     * 
     * @param itemId 巡检项主键
     * @return 结果
     */
    @Override
    public int deleteSbInspectionItemByItemId(Long itemId)
    {
        return sbInspectionItemMapper.deleteSbInspectionItemByItemId(itemId);
    }

    /**
     * 批量保存巡检项
     * 
     * @param configId 巡检配置ID
     * @param itemList 巡检项列表
     * @param username 操作用户
     * @return 结果
     */
    @Override
    @Transactional
    public int batchSaveInspectionItems(Long configId, List<SbInspectionItem> itemList, String username)
    {
        // 先删除原有的巡检项
        SbInspectionItem query = new SbInspectionItem();
        query.setConfigId(configId);
        List<SbInspectionItem> oldItems = selectSbInspectionItemList(query);
        
        for (SbInspectionItem item : oldItems) {
            deleteSbInspectionItemByItemId(item.getItemId());
        }
        
        // 再添加新的巡检项
        Date now = DateUtils.getNowDate();
        for (SbInspectionItem item : itemList) {
            item.setConfigId(configId);
            // 保留原有排序，不再设置新排序
            if (item.getSortOrder() == null) {
                item.setSortOrder(0);  // 设置默认值为0
            }
            item.setCreateBy(username);
            item.setCreateTime(now);
            item.setUpdateBy(username);
            item.setUpdateTime(now);
            
            // 设置默认值
            if (item.getDataType() == null) {
                item.setDataType("0");  // 默认为数字类型
            }
            if (item.getIsRequired() == null) {
                item.setIsRequired("1");  // 默认为必填
            }
            if (item.getRequireImage() == null) {
                item.setRequireImage("0");  // 默认不需要图片
            }
            
            item.setIsDelete("0");
        }
        
        // 批量插入
        if (!itemList.isEmpty()) {
            return sbInspectionItemMapper.batchInsertSbInspectionItem(itemList);
        }
        
        return 0;
    }
} 